package io.renren.modules.maintenance.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.modules.maintenance.entity.MDeviceCategory;
import io.renren.modules.maintenance.mapper.MDeviceCategoryMapper;
import io.renren.modules.maintenance.service.IMDeviceCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author iweb
 * @since 2020-08-07
 */
@Service
public class MDeviceCategoryServiceImpl extends ServiceImpl<MDeviceCategoryMapper, MDeviceCategory> implements IMDeviceCategoryService {

    @Autowired
    private IMDeviceCategoryService imDeviceCategoryService;

    /**
     * 返回所有设备种类的list
     * @return
     */
    @Override
    public List<MDeviceCategory> getAll() {
        return imDeviceCategoryService.list();
    }

    /**
     * 向数据库插入新的设备种类
     * @param deviceCategory
     * @return
     */
    @Override
    public boolean insertDevCate(String deviceCategory) {
        if(deviceCategory.isEmpty()){
            return false;
        }

        String category = JSONObject.parseObject(deviceCategory).getString("deviceCategory");

        String[] split = category.split("-");
        MDeviceCategory mDeviceCategory = new MDeviceCategory();
        mDeviceCategory.setRobotType(split[0]);
        mDeviceCategory.setRobotSeries(split[1]);
        boolean save = imDeviceCategoryService.save(mDeviceCategory);
        return save;
    }
}
